<?php
class Project_management extends CI_Model{
	
	function __construct(){
	 // Call the Model constructor
        parent::__construct();
		$this->load->database();
	}

	function generateProjectCode(){
		$this->db->select('projectCode');
		$this->db->order_by('projectCode desc');
		$result=$this->db->get('projects');
		if($result->num_rows() > 0){
			$lastProjectCode = $result->row()->projectCode;
			$newProjectCode = explode('P',$lastProjectCode);
			return 'P'.($newProjectCode[1]+1);
		}
		else{
			return 'P1001';
		}
	}

	function generateProjectStageID(){
		$this->db->select('stageID');
		$this->db->order_by('stageID desc');
		$result=$this->db->get('project_stage');
		if($result->num_rows() > 0){
			$lastProjectCode = $result->row()->stageID;
			$newProjectCode = explode('PS',$lastProjectCode);
			$newStageId = explode('/',$newProjectCode[1]);
			return 'PS'.($newStageId[1]+1);
		}
		else{
			return 'PS1001/1';
		}
	}

	function createProject($data){
		$this->db->insert('projects',$data);
		return $this->db->affected_rows();
	}
	function updateProject($data){
		$this->db->where('projectCode',$data['projectCode']);
		$this->db->update('projects',$data);
		return $this->db->affected_rows();
	}
	function createProjectStage($data){
		$this->db->insert('project_stage',$data);
		return $this->db->affected_rows();
	}

	function getProjects(){
		$result = $this->db->query('select * from projects');
		return $result->result();
	}
	function getProject($project_id){
		$query = "select * from projects where projectCode='".$project_id."'";
		$result = $this->db->query($query);
		return $result->result();
	}
	function getStages($project_id){
		$query = "select * from project_stage where projectCode ='".$project_id."'";
		$result = $this->db->query($query);
		return $result->result();
	}


}
?>